package com.zmn.plat.business.impl.online.service.group.city;

import com.zmn.common.utils.date.DateUtil;
import com.zmn.plat.business.interfaces.online.service.group.city.OnlineServiceGroupCityBService;
import com.zmn.plat.model.entity.online.service.group.OnlineServiceGroup;
import com.zmn.plat.model.entity.online.service.group.city.OnlineServiceGroupCity;
import com.zmn.plat.services.interfaces.online.service.group.OnlineServiceGroupService;
import com.zmn.plat.services.interfaces.online.service.group.city.OnlineServiceGroupCityService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author linfeng
 * @description 在线客服分组城市配置BService实现类
 * @modifier
 * @since 2021/3/29 16:00
 **/
@Service
public class OnlineServiceGroupCityBServiceImpl implements OnlineServiceGroupCityBService {
    @Resource
    private OnlineServiceGroupCityService onlineServiceGroupCityService;
    @Resource
    private OnlineServiceGroupService onlineServiceGroupService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    @SuppressWarnings("Duplicates")
    public void configCity(List<OnlineServiceGroupCity> list, String operator) {
        // 先删除该分组下的城市数据
        onlineServiceGroupCityService.deleteByGroupId(list.get(0).getGroupId());

        list.forEach(e -> {
            e.setCreater(operator);
            e.setCreateTime(DateUtil.getNow());
            e.setUpdater(operator);
            e.setUpdateTime(DateUtil.getNow());
        });

        onlineServiceGroupCityService.insertBatch(list);

        Integer groupId = list.get(0).getGroupId();
        OnlineServiceGroup onlineServiceGroup = onlineServiceGroupService.findByKey(groupId);
        onlineServiceGroup.setUpdater(operator);
        onlineServiceGroup.setUpdateTime(DateUtil.getNow());
        onlineServiceGroupService.updateByKey(onlineServiceGroup);
    }
}